﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Entry;
using System.Drawing;
using COFE.Core.Utils;
using COFE.Core.Validater;

namespace COFE.Core.Lister
{
    /// <summary>
    /// Provide IconProperties.SmallThumbnail, Thumbnail and LargeThumbnail property.
    /// </summary>
    public class ThumbnailPropertyProvider : ISecondaryPropertyProvider<IFileSystemInfo>
    {
        #region Const

        public static string SupportedExtensions = ".jpg,.jpeg,.bmp,.gif,.png";

        #endregion 

        #region Constructor

        #endregion

        #region Methods

        public object[] GetSupportedProperty(IFileSystemInfo sourceObj)
        {
            object[] imageProperties = new object[] {
                IconProperties.Thumbnail,
                IconProperties.SmallThumbnail, 
                IconProperties.MediumThumbnail, 
                IconProperties.LargeThumbnail
            };            

            if (sourceObj is IFileInfo)
            {
                var supExt = SupportedExtensions.Split(',');

                if (supExt.Contains(COFEPath.GetExtension(sourceObj.ParseName).ToLower()))
                    return imageProperties;
            }

            return new object[] { };
        }

        public bool IsPropertyReadOnly(IFileSystemInfo sourceObj, object property)
        {
            return true;            
        }

        public PropertyPair GetProperty(IFileSystemInfo sourceObj, object property)
        {
            if (property == null)
                throw new ArgumentException();

            if (sourceObj is IFileInfo)
            {
                if ((int)property == (int)IconProperties.Thumbnail)
                    throw new NotImplementedException();
                //return new PropertyPair(property,
                //        ResourceListAttributeExtension.GetResourceList((IPropertyHost)sourceObj, property));          

                var bitmapSizeAttribute =
                    EnumUtils<BitmapSizeAttribute>.FindAttribute(property);
                if (bitmapSizeAttribute == null)
                    throw new ArgumentException(property.ToString() + " does not support BitmapSizeAttribute.");

                using (var stream = (sourceObj as IFileInfo).OpenRead())
                {
                    return 
                        new PropertyPair(property, 
                        Image.FromStream(stream).GetThumbnailImage(
                        bitmapSizeAttribute.Width, bitmapSizeAttribute.Height, () => false,
                        IntPtr.Zero));
                }
            }

            throw new NotSupportedException();
        }

        public void SetProperty(IFileSystemInfo sourceObj, object property, object value)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        #endregion

        
    }
}
